var bezig = false;
var oAjax = null;
var YAHOO = {
    Finance:{
        SymbolSuggest:{
            
        }
    }
}
//$('#zoekBedrijf').val(term);testval
$(document).ready(function(){
    $('#updateDetail').bind({
        click: function(){
          if (!bezig){//split and mirge
              bezig = true;
              var src = $('#grafiek').attr('src');
              src = src.split('&');
              //@see model/detail_index  line+/-11 'http://chart.finance.yahoo.com/z?s='.$bedrijf.'&t=6m&q=1&l=on&z=1&p=m50,m200';
              
              var tijd= $('.typetijd:checked').attr('id');
              
              var grafiek= $('.typegrafiek:checked').attr('id');
             
              var schaal= $('.typeschaal:checked').attr('id');
              src[1] = 't='+ tijd;
              src[2] = 'q='+ grafiek;
              src[3] = 'l='+ schaal;
              src = src.join('&');
              $('#grafiek').attr('src',src);
              bezig = false;
            }   
        }
    });
    $('#zoekBedrijf').bind({
        keyup: function(e){
            var term = $.trim($('#zoekBedrijf').val());
                term = term.toUpperCase();
                $('#zoekBedrijf').val(term);
            if(!bezig && term.length > 1){
                bezig = true;
                url = encodeURIComponent(term);
                oAjax =  $.ajax({
                    url:"http://autoc.finance.yahoo.com/autoc",
                    type:"GET",
                    jsonp:'callback',
                    jsonpCallback:'YAHOO.Finance.SymbolSuggest.ssCallback',
                    dataType:'jsonp',
                    data: {query:url},
                    error:fout,
                    timeout:2000
                })
            }
        }
    });
});
YAHOO.Finance.SymbolSuggest.ssCallback = function(data){
    var resultaat = data.ResultSet.Result;
    //console.log(resultaat);
    var html = '<table class="table table-striped">';
        html += '<thead><tr><td>Symbool</td><td>Bedrijf</td>';
        html += '<td>Exchange</td><td>Equity</td></tr></thead>';
        html += '<tbody>';
    
    $.each(resultaat,function(ndx,el){
        html += '<tr>';
            html += '<td>';
            html += '<a href="/Overzicht/'+el['symbol']+'">'+el['symbol']+'</a>';
            html += '</td>';
            html += '<td>'+el['name']+'</td>';
            html += '<td>'+el['exchDisp']+'</td>';
            html += '<td>'+el['typeDisp']+'</td>';
        html += '</tr>';
    });
    html += '<tbody></table>';
    $('#resultaat').html(html);
    fout();
};
function fout(){
    oAjax.abort();
    oAjax = null;
    bezig = false;
}